<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\GoodsShelf;
use App\Models\ModelNumber;
use App\Models\Product;
use App\Models\ProductModelNumber;
use App\Models\Specification;
use App\Models\WarehouseLocation;
use App\Rep\ModelNumberRep;
use App\Rep\OrderRep;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddProductKucun extends Command
{
    /**
     * 添加成品库存
     * php artisan command:addproductkucun
     * @var string
     */
    protected $signature = 'command:addproductkucun';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //$file = public_path('excel').'/库存盘点.xlsx';//获取文件路径
        $file = public_path('excel').'/成品库存.xlsx';//获取文件路径
        $import = new MultipleImport(3);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        unset($results[0]);
        $orderRep = new OrderRep();
        foreach($results as $k=>$v){
            if(empty($v[0])){
                continue;
            }
            $this->info('导入：产品名称='.$v[0].";型号=".$v[1].";规格=".$v[2].";库存数量=".$v[3].";仓位=".$v[4]);
            $product = Product::where('product_name',$v[0])->first();
            $model_number = ModelNumber::updateOrCreate(['model_number'=>$v[1]],['model_number'=>$v[1]]);
            ProductModelNumber::updateOrCreate(
                ['product_id'=>$product->id,'model_number_id'=>$model_number->id],
                ['product_id'=>$product->id,'model_number_id'=>$model_number->id]
            );
            if(!empty($v[2])){
                if($v[2] == '2'){
                    $v[2] = '2*2.2米';
                }elseif($v[2] == '1.8'){
                    $v[2] ='1.8*2.2米';
                }
                $specification = Specification::updateOrCreate(['specification'=>$v[2]],['specification'=>$v[2]]);
                $specification_id = $specification->id;
            }else{
                $specification_id = '';
            }
            $warehouse_id = 2;
            $goods_shelf_id = 3;
            if($v[0] == '软床'){
                $goods_shelf_id = 3;
            }elseif($v[0] == '沙发'){
                $goods_shelf_id = 4;
            }
            $warehouse_location_obj = WarehouseLocation::updateOrCreate(
                ['warehouse_id'=>$warehouse_id,'goods_shelf_id'=>$goods_shelf_id,'warehouse_location'=>$v[4]],
                ['warehouse_id'=>$warehouse_id,'goods_shelf_id'=>$goods_shelf_id,'warehouse_location'=>$v[4]]
            );
            $modelList = (new ModelNumberRep())->getProductInfoByModelNumberIdRep($model_number->id,$product->modelid);
            $semi_product = [];
            foreach($modelList as $kk=>$vv){
                $semi_product[] = [
                    "modelid"=>$product->modelid,
                    "warehouse_id"=>$warehouse_id,
                    "goods_shelf_id"=>$goods_shelf_id,
                    'warehouse_location_id'=>$warehouse_location_obj->id,
                    "product_id"=>$product->id,
                    "model_number_id"=>$model_number->id,
                    "specification_id"=>$specification_id,
                    "num"=>1,
                    "is_kucun"=>2
                ];
            }
            for($i=0;$i<$v[3];$i++){
                $data = [
                    'modelid'=>$product->modelid,
                    'num'=>1,
                    "warehouse_id"=>$warehouse_id,
                    "goods_shelf_id"=>$goods_shelf_id,
                    'warehouse_location_id'=>$warehouse_location_obj->id,
                    "batch_number"=>'',
                    "serial_number"=>$orderRep->automaticGenerationSerialNumberRep($i+1),
                    "order_number"=>$orderRep->automaticGenerationOrderNumberRep($i+1),
                    "order_date"=>date('Y-m-d H:i:s'),
                    "delivery_date"=> date('Y-m-d H:i:s'),
                    "account_executive_id"=>'',
                    "product_id"=>$product->id,
                    "model_number_id"=>$model_number->id,
                    "color_id"=> null,
                    "size"=> "",
                    "is_chuangtougui"=> 0,
                    "specification_id"=> $specification_id,
                    "direction"=> "",
                    "customer_id"=> 0,
                    "received"=> "",
                    "emergency_procedure"=> "1",
                    "is_replenishment"=> "1",
                    "is_stock_up"=> "1",
                    "receivables"=> 0,
                    "remark"=> "成品盘点批量导入",
                    "id"=> "",
                    "is_kucun"=> "2",
                    "unit_price"=> "",
                    "discount"=> "",
                    "spare_money"=> 0,
                    "customer_name"=> "",
                    "account_executive"=> "",
                    "model_number"=> $v[1],
                    "pid"=> 1,
                    "semi_product"=> $semi_product
                ];
                $params['order_data'][] = $data;
            }
            $params['user_id'] = 1;

        }
        $res = $orderRep->createOrderRep($params);
        $this->info('success！！！'.json_encode($res));
    }
}
